﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using PortableVersion.Core.General;

namespace PortableVersion.Core.FileSystem
{
    public static class FileSystemHelper
    {
        public static IEnumerable<string> GetSupportedTypes(string typesList)
        {
            typesList.AssertNotNull("typesList");
            var rv = typesList.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            rv.AssertAll(IsValidExtension, t =>
                                               {
                                                   throw new InvalidExpressionException(
                                                       String.Format("Extension {0} is not valid.", t));
                                               });
            return rv;
        }

        /// <summary>
        /// Checks if the extension is valid. TODO: use the right business logic
        /// </summary>
        public static bool IsValidExtension(string ext)
        {
            return !ext.Contains("*");
        }

        public static string ReplaceCommonPath(string sourceBaseDir, string targetBaseDir, string sourceItemPath)
        {
            if (!Path.IsPathRooted(sourceBaseDir) || !Path.IsPathRooted(targetBaseDir) || !Path.IsPathRooted(sourceItemPath))
            {
                throw new ArgumentException("GetTargetPath can only work with absolute paths.");
            }
            if (!sourceItemPath.ToLowerInvariant().Substring(0, sourceBaseDir.Length).Equals(sourceBaseDir.ToLowerInvariant()))
            {
                throw new ArgumentException(String.Format(@"Source item '{0}' is not under source directory '{1}'",
                                                          sourceItemPath, sourceBaseDir));
            }
            return Path.Combine(targetBaseDir, sourceItemPath.Substring(sourceBaseDir.Length + 1));
        }
    }
}
